<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Logistics\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Logistics;
use App\Eloquent\Ygt\LogisticsBill;
use App\Eloquent\Ygt\LogisticsNotes;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class BillController extends Controller
{
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',25);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $logisticsId        = request('id',0);
        if( !$userInfo || !$logisticsId ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['id'=>$logisticsId];
        $logistics          = Logistics::getInfo($where);
        $logisticsName      = $logistics->title ? $logistics->title : '未知快递';
        $where              = ['company_id'=>$companyId,'logistics_id'=>$logisticsId];
        $collection         = LogisticsNotes::getList($where);
        $notesListObj       = $collection->toArray();
        $notesList          = [];
        foreach($notesListObj as $key=>$val)
        {
            $arr            = json_decode(json_encode($val),true);
            $notesList[]    = $arr;
        }
        foreach($notesList as $key=>$val)
        {
            $totalMoney         = $val['weight'] * $val['freight'];
            $order              = LogisticsNotes::getCustomerOrder($val['customer_order_id'],1);
            $notesList[$key]['order']           = $order;
            $notesList[$key]['status_name']   = '发货中';
            $notesList[$key]['logistics_name']   = $logisticsName;
            $notesList[$key]['total_money']   = $totalMoney;
        }
        return $notesList;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',1);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if( !$userInfo ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $isSubmit           = request('is_submit',0);
        //$notesId            = request('id',0);
        if($isSubmit){
            $data                   = request()->all();
            $data['company_id']   = $companyId;
            $fieldList              = LogisticsBill::$tableFieldList;
            $tableData              = LogisticsBill::setDataByTableField($data,$fieldList);
            $tableDataCheckResult   = LogisticsBill::checkDataByTableField($tableData,$fieldList);
            if($tableDataCheckResult !== true)
            {
                xThrow(ERR_PARAMETER, $tableDataCheckResult);
            }
            $editResult             = LogisticsBill::edit($tableData);
            if($editResult===false)
            {
                xThrow(ERR_UNKNOWN);
            }
            $result                 = ['message'=>'编辑成功'];
            return $result;
        } else {
            $result                 = [];
            return $result;
        }
    }
    public function detail()
    {
        $id             = request('id',0);
        $where          = ['id'=>$id];
        $info           = LogisticsNotes::getInfoData($where);
        if($info){
            //相关订单
            $orderList      = LogisticsNotes::getCustomerOrder($info['customer_order_id'],2);
            //付款记录列表
            $where          = ['notes_id'=>$id];
            $billList       = LogisticsBill::getLists($where);
            $billMoney      = 0;
            foreach($billList as $key=>$val)
            {
                $billMoney  += $val['money'];
            }
            $remainMoney    = $info['total_money'] - $billMoney;
            $info['remain_money']   = $remainMoney;
            $result         = ['info'=>$info,'order_list'=>$orderList,'bill_list'=>$billList];
            return $result;
        }else{
            xThrow(ERR_UNKNOWN);
        }
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if( !$userInfo || !$id ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'id'=>$id];
        $data               = ['status'=>2];
        $result             = LogisticsBill::updateOneData($where,$data);
        if($result){
            return ['删除成功'];
        }else{
            xThrow(ERR_UNKNOWN);
        }
    }
}